package top.dingyingying.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.dingyingying.springboot3.aop.AuthInfo;
import top.dingyingying.springboot3.base.PageBean;
import top.dingyingying.springboot3.base.R;
import top.dingyingying.springboot3.dao.TbContactMapper;
import top.dingyingying.springboot3.entity.TbContact;
import top.dingyingying.springboot3.interceptor.AppInterceptor;
import top.dingyingying.springboot3.service.TbContactService;
import top.dingyingying.springboot3.vo.PageVo;

@Service
@RequiredArgsConstructor
@Slf4j
public class TbContactServiceImpl implements TbContactService {
    private final TbContactMapper tbContactMapper;

    @Override
    public R<String> insert(TbContact tbContact) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        tbContact.setUid(authInfo.getUid());
        int i = tbContactMapper.insert(tbContact);
        QueryWrapper<TbContact> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", authInfo.getUid());
        return i == 1 ? R.ok("添加成功", "") : R.fail("添加失败", "");

    }

    @Override
    public R<PageVo<TbContact>> query(TbContact tbContact, PageBean pageBean) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        QueryWrapper<TbContact> wrapper = new QueryWrapper<>();
        wrapper.select("id", "uid", "username", "phone", "email", "address");
        wrapper.like(StringUtils.hasText(tbContact.getUsername()), "username", tbContact.getUsername());
        wrapper.like(StringUtils.hasText(tbContact.getPhone()), "phone", tbContact.getPhone());
        wrapper.like(StringUtils.hasText(tbContact.getEmail()), "email", tbContact.getEmail());
        wrapper.like(StringUtils.hasText(tbContact.getAddress()), "address", tbContact.getAddress());
        wrapper.eq("uid", authInfo.getUid());
        IPage<TbContact> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        page = tbContactMapper.selectPage(page, wrapper);
        return R.ok(page);

    }

    @Override
    public R<String> delete(Integer id) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        QueryWrapper<TbContact> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("uid", authInfo.getUid());
        TbContact tbContact = tbContactMapper.selectById(id);
        if (tbContact == null) {
            return R.fail("联系人不存在", null);
        }
        if (!tbContact.getUid().equals(authInfo.getUid())) {
            return R.fail("只能删除自己的联系人", null);
        }
        int i = tbContactMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }

    @Override
    public R<TbContact> queryById(Integer id) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        QueryWrapper<TbContact> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("uid", authInfo.getUid());
        TbContact tbContact = tbContactMapper.selectOne(wrapper);
        if (tbContact == null) {
            return R.fail("联系人不存在", null);
        }
        return R.ok("", tbContact);

    }

    @Override
    public R<String> update(TbContact tbContact) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        QueryWrapper<TbContact> wrapper = new QueryWrapper<>();
        wrapper.eq("id", tbContact.getId());
        wrapper.eq("uid", authInfo.getUid());
        TbContact tbContact1 = tbContactMapper.selectOne(wrapper);
        if (tbContact1 == null) {
            return R.fail("联系人不存在", null);
        }
        int i = tbContactMapper.updateById(tbContact);
        return i == 1 ? R.ok("更新成功", null) : R.fail("更新失败", null);

    }

}
